/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.listener;

import com.yami.shop.bean.event.PaySuccessOrderEvent;
import com.yami.shop.bean.order.SubmitOrderOrder;
import com.yami.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 支付成功后的默认操作
 *
 * @author LGH
 */
@Component("defaultPaySuccessListener")
@AllArgsConstructor
public class PaySuccessListener {
    @Autowired
    private UserService userService;

    /**
     * 扣除优惠券和积分
     */
    @EventListener(PaySuccessOrderEvent.class)
    @Order(SubmitOrderOrder.DEFAULT)
    public void defaultSubmitOrderListener(PaySuccessOrderEvent event) {
        List<com.yami.shop.bean.model.Order> orders = event.getOrders();
        for (com.yami.shop.bean.model.Order order : orders) {
            //    扣除该用户的优惠券
            String userId = order.getUserId();
            Integer couponCount = order.getCouponCount();
            String orderNumber = order.getOrderNumber();
            if(couponCount !=0){
                userService.subCouponByUid(userId, couponCount, "订单编号：" + orderNumber);
            }

        //    扣除积分
            userService.subScoreByUid(userId,order.getActualTotal(),"订单编号："+orderNumber);
        }

    }

}
